import * as userService from '../services/system/UserService';
import { judgePermission,isloadPermission,setPermission } from '../common/shiro';
import { Constants } from '../common/constant/constants';
export default {
  namespace: 'user',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    roleIdList:[],
    loading: false,
    // success:true,
    permission:{},
  },

  effects: {
    *judgePermission(_, { call, put }){
      if(!isloadPermission()){
        const response = yield call(userService.getPermissions);
        setPermission(response.result)
      }
      var permission = judgePermission(Constants.SYSTEM_USER.PERM);
      yield put({
        type: 'savePermission',
        payload:permission,
      });
      return permission;
    },
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(userService.queryUsers, payload);
      const returnData = {
        list:response.result.list,
        pagination:{
          pageSize:response.result.pageSize,
          total:response.result.total,
          pageIndex:payload.pageIndex,          
        },
      }
      yield put({
        type: 'save',
        payload: returnData,
      });
    },
    *fetchUserRole({ payload }, { call, put }){
      const response = yield call(userService.queryUserInfo, payload);
      // yield put({
      //   type:'saveRoleIds',
      //   payload:response.result.roleIdList,
      // });
      return response.result.roleIdList;
    },

    *post({ payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      let response;
      if(payload.userId==0){//新建
        response = yield call(userService.saveUser,payload);
      }else{//修改
        response = yield call(userService.updateUser,payload);
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      return response;
    },

    *delete({ payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(userService.deleteUser,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      return response;
    },
    *export(_, { call, put }){
      const response = yield call(userService.exportUser);
      return;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
        loading: false,
      };
    },
    savePermission(state,{payload}){
      return {
        ...state,
        permission:payload,
      }
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    // changeSuccess(state, { payload }){
    //   return {
    //     ...state,
    //     loading:false,
    //     success:payload,
    //   };
    // },
    saveRoleIds(state,action){
      return{
        ...state,
        roleIdList:action.payload,
      }
    },
  },
};
